package com.crazy.web.service.share.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crazy.model.major.entity.TLabel;
import com.crazy.model.major.entity.TUser;
import com.crazy.model.major.entity.TUserRoleRelation;
import com.crazy.model.major.service.TUserRoleRelationService;
import com.crazy.model.major.service.TUserService;
import com.crazy.web.service.share.UserReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/12/14 10:33
 */
@Service
public class UserReviewServiceImpl implements UserReviewService {

    @Autowired
    private TUserService tUserService;
    @Autowired
    private TUserRoleRelationService tUserRoleRelationService;

    @Override
    public IPage<TUser> find(Long userId, int page, int rows, String search) {
        QueryWrapper<TUser> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.like(TUser.Columns.userName, search);
        QueryWrapper<TUser> queryWrapper2 = queryWrapper1.or();
        queryWrapper2.like(TUser.Columns.nickName, search);
        queryWrapper1.orderByDesc(TUser.Columns.id);
        Page<TUser> userPage = new Page<TUser>(page, rows);
        IPage<TUser> userIPage = tUserService.page(userPage, queryWrapper1);
        return userIPage;
    }

    @Override
    public void lock(Long userId, int toLock) {
        TUser tUser = tUserService.getById(userId);
        tUser.setLocked(toLock);
        tUserService.updateById(tUser);
    }

    @Override
    public void tiedRole(Long userId, Long[] roles) {
        if(roles != null){
            List<TUserRoleRelation> list = new ArrayList<>();
            for(Long role : roles){
                TUserRoleRelation userRoleRelation = new TUserRoleRelation();
                userRoleRelation.setUserId(userId);
                userRoleRelation.setRoleId(role);
                list.add(userRoleRelation);
            }
            tUserRoleRelationService.saveBatch(list);
        }
    }

}
